function add0(m) {
    return m<10?'0'+m:m;
}
//获取时间戳
function now() {
    return ~~(Date.now()/1000);
}
function nowMs() {
    return Date.now();
}
function nowStr() {
    let time = new Date();
    return datetime(time);
}
//今日零点时间戳
function dayZero(timeZone=8) {
    let tt = ~~(Date.now()/1000);
    return tt - tt % 86400 - timeZone * 3600;
}
//字符串转时间戳"2015/06/23 08:00:20"或"2015-06-23 08:00:20"
function getTimestamp(str) {
    return ~~((new Date(str)).valueOf()/1000);
}
function getTimestampMs(str) {
    return (new Date(str)).valueOf();
}
//获取时间日期字符串（毫秒时间戳）
function datetimeUTC(timestamp, timeZone=0)
{
    if (timeZone!=0) {
        timestamp += timeZone*3600000;
    }
    let time = new Date(timestamp);
    let y = time.getUTCFullYear();
    let m = time.getUTCMonth()+1;
    let d = time.getUTCDate();
    let h = time.getUTCHours();
    let mm = time.getUTCMinutes();
    let s = time.getUTCSeconds();
    return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
function datetimeLocal(timestamp)
{
    let time = new Date(timestamp);
    return datetime(time);
}
function datetime(date)
{
    if (!date) {
        return null;
    }
    let y = date.getFullYear();
    let m = date.getMonth()+1;
    let d = date.getDate();
    let h = date.getHours();
    let mm = date.getMinutes();
    let s = date.getSeconds();
    return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
//得到标准时区的时间戳
function UTC(timeZone) {
    //参数i为时区值数字，比如北京为东八区则输进8,纽约为西5区输入-5
    if (typeof timeZone !== 'number') return 0;
    let d = new Date();
    //得到1970年一月一日到现在的秒数
    let len = d.getTime();
    //本地时间与GMT时间的时间偏移差
    let offset = d.getTimezoneOffset() * 60000;
    //得到现在的格林尼治时间
    let utcTime = len + offset;
    //return new Date(utcTime + 3600000 * getLocalTime);
    return utcTime + 3600000 * timeZone;
}
//获取日期字符串（毫秒时间戳）
function dateLocalStamp(timestamp)
{
    let time = new Date(timestamp);
    return dateLocal(time);
}
function dateLocal(date)
{
    if (!date) {
        return null;
    }
    let y = date.getFullYear();
    let m = date.getMonth()+1;
    let d = date.getDate();
    return y+'-'+add0(m)+'-'+add0(d);
}
//获取日期小时字符串（毫秒时间戳）
function dateHourLocalStamp(timestamp)
{
    let time = new Date(timestamp);
    let y = time.getFullYear();
    let m = time.getMonth()+1;
    let d = time.getDate();
    let h = time.getHours();
    return y+'-'+add0(m)+'-'+add0(d)+'-'+add0(h);
}
//获取时间字符串（毫秒时间戳）
function timeLocalStamp(timestamp)
{
    let time = new Date(timestamp);
    return time.toLocaleTimeString([],{hour12:false});
}
function timeLocal(date)
{
    if (!date) {
        return null;
    }
    return date.toLocaleTimeString([],{hour12:false});
}
//获取年月日整数
function getDateNumber(date)
{
    if (!date) {
        return null;
    }
    let y = date.getFullYear();
    let m = date.getMonth()+1;
    let d = date.getDate();
    return y*10000+m*100+d;
}

// console.log(now());
// console.log(UTC(8));
// console.log(dayZero());
// console.log(datetime(1543286391000, 8));
// console.log(datetimeLocal(1543286391000));
// console.log(getTimestampMs("2018-11-27 18:39:51"));
// console.log(getTimestampMs("2018-11-27 18:39:51", 8));
// console.log(getLocalTime(8));
// console.log(timeLocalStamp(Date.now()));
// console.log(nowStr());
// console.log(getDateNumber(new Date));

exports.now = now;
exports.nowMs = nowMs;
exports.nowStr = nowStr;
exports.dayZero = dayZero;
exports.getTimestamp = getTimestamp;
exports.getTimestampMs = getTimestampMs;
exports.datetimeLocal = datetimeLocal;
exports.datetime = datetime;
exports.datetimeUTC = datetimeUTC;
exports.UTC = UTC;
exports.dateLocalStamp = dateLocalStamp;
exports.dateLocal = dateLocal;
exports.dateHourLocalStamp = dateHourLocalStamp;
exports.timeLocalStamp = timeLocalStamp;
exports.timeLocal = timeLocal;
exports.getDateNumber = getDateNumber;